import type { ConfigEnv, UserConfig } from 'vite'

import { useApplicationConfig } from '@fl/vite-config'
import UnoCSS from 'unocss/vite'
import { loadEnv, mergeConfig } from 'vite'

export default ({ command, mode }: ConfigEnv): UserConfig => {
    const applicationConfig = useApplicationConfig({ command, mode })

    const root = process.cwd()
    const env = loadEnv(mode, root)

    const overrides = {
        plugins: [
            UnoCSS(),
        ],
        preview: {
            port: 6130,
        },
        server: {
            // 端口号
            port: 6030,
            proxy: {
                '/bidding-api': {
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/bidding-api/, ''),
                    target: env.VITE_API_TENDER_PATH,
                },

                '/mall': {
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/mall/, ''),
                    target: env.VITE_API_MALL_PATH,
                },

            },
        },
    }

    return mergeConfig(applicationConfig, overrides)
}
